package com.macbulldev.poc.testgin.domain;

import java.math.BigDecimal;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.EntityManager;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.PersistenceContext;
import javax.persistence.Version;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.transaction.annotation.Transactional;


@Configurable
@javax.persistence.Entity
@org.springframework.roo.addon.javabean.RooJavaBean
@org.springframework.roo.addon.tostring.RooToString
@org.springframework.roo.addon.entity.RooEntity
public class LiabilityAmount {

    private String importName;

    private java.math.BigDecimal amount;
    
    private java.math.BigDecimal premium;
    
    @javax.persistence.ManyToOne(targetEntity = com.macbulldev.poc.testgin.domain.TrialCase.class)
    @javax.persistence.JoinColumn(name="trialCaseId")
    private com.macbulldev.poc.testgin.domain.TrialCase trialCase;
    
    @javax.persistence.ManyToOne(targetEntity = com.macbulldev.poc.testgin.domain.TrialGroup.class)
    @javax.persistence.JoinColumn(name="trialGroupId")
    private com.macbulldev.poc.testgin.domain.TrialGroup trialGroup;
    
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "liabilityAmount")
    private Set<TransactionQuestion> transactionQuestions;
    
    @javax.persistence.ManyToOne(targetEntity = com.macbulldev.poc.testgin.domain.PolicyForm.class)
    @javax.persistence.JoinColumn(name="policyFormId")
    private com.macbulldev.poc.testgin.domain.PolicyForm policyForm;

	@PersistenceContext    
    transient EntityManager entityManager;

	@Id    
    @GeneratedValue(strategy = GenerationType.AUTO)    
    @Column(name = "id")    
    private Long id;

	@Version    
    @Column(name = "version")    
    private Integer version;

	public Long getId() {    
        return this.id;        
    }

	public void setId(Long id) {    
        this.id = id;        
    }

	public Integer getVersion() {    
        return this.version;        
    }

	public void setVersion(Integer version) {    
        this.version = version;        
    }

	@Transactional    
    public void persist() {    
        if (this.entityManager == null) this.entityManager = entityManager();        
        this.entityManager.persist(this);        
    }

	@Transactional    
    public void remove() {    
        if (this.entityManager == null) this.entityManager = entityManager();        
        if (this.entityManager.contains(this)) {        
            this.entityManager.remove(this);            
        } else {        
            LiabilityAmount attached = this.entityManager.find(LiabilityAmount.class, this.id);            
            this.entityManager.remove(attached);            
        }        
    }

	@Transactional    
    public void flush() {    
        if (this.entityManager == null) this.entityManager = entityManager();        
        this.entityManager.flush();        
    }

	@Transactional    
    public void merge() {    
        if (this.entityManager == null) this.entityManager = entityManager();        
        LiabilityAmount merged = this.entityManager.merge(this);        
        this.entityManager.flush();        
        this.id = merged.getId();        
    }

	public static final EntityManager entityManager() {    
        EntityManager em = new LiabilityAmount().entityManager;        
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");        
        return em;        
    }

	public static long countLiabilityAmounts() {    
        return (Long) entityManager().createQuery("select count(o) from LiabilityAmount o").getSingleResult();        
    }

	public static List<LiabilityAmount> findAllLiabilityAmounts() {    
        return entityManager().createQuery("select o from LiabilityAmount o").getResultList();        
    }

	public static LiabilityAmount findLiabilityAmount(Long id) {    
        if (id == null) throw new IllegalArgumentException("An identifier is required to retrieve an instance of LiabilityAmount");        
        return entityManager().find(LiabilityAmount.class, id);        
    }

	public static List<LiabilityAmount> findLiabilityAmountEntries(int firstResult, int maxResults) {    
        return entityManager().createQuery("select o from LiabilityAmount o").setFirstResult(firstResult).setMaxResults(maxResults).getResultList();        
    }

	public String getImportName() {    
        return this.importName;        
    }

	public void setImportName(String importName) {    
        this.importName = importName;        
    }

	public BigDecimal getAmount() {    
        return this.amount;        
    }

	public void setAmount(BigDecimal amount) {    
        this.amount = amount;        
    }

	public TrialCase getTrialCase() {    
        return this.trialCase;        
    }

	public void setTrialCase(TrialCase trialCase) {    
        this.trialCase = trialCase;        
    }

	public TrialGroup getTrialGroup() {    
        return this.trialGroup;        
    }

	public void setTrialGroup(TrialGroup trialGroup) {    
        this.trialGroup = trialGroup;        
    }

	public PolicyForm getPolicyForm() {    
        return this.policyForm;        
    }

	public void setPolicyForm(PolicyForm policyForm) {    
        this.policyForm = policyForm;        
    }
	
	public BigDecimal getPremium() {    
        return this.premium;        
    }

	public void setPremium(BigDecimal premium) {    
        this.premium = premium;        
    }
	
	public List<TransactionQuestion> getTransactionQuestions() {  
		
		EntityManager em = new TransactionQuestion().entityManager;
		return em.createQuery("select o from TransactionQuestion o where o.liabilityAmount.id = ? order by o.Type desc").setParameter(1, this.id).getResultList();
		
        //return this.transactionQuestions;        
    }

	public void setTransactionQuestions(Set<TransactionQuestion> transactionQuestions) {    
        this.transactionQuestions = transactionQuestions;        
    }

	public String toString() {    
        StringBuilder sb = new StringBuilder();        
        sb.append("Id: ").append(getId()).append(", ");        
        sb.append("Version: ").append(getVersion()).append(", ");        
        sb.append("ImportName: ").append(getImportName()).append(", ");
        sb.append("Premium: ").append(getPremium()).append(", ");
        sb.append("Amount: ").append(getAmount()).append(", ");        
        sb.append("TrialCase: ").append(getTrialCase());        
        return sb.toString();        
    }
}
